package com.jin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jin.bean.Unit;
import com.jin.common.ResponseResult;
import com.jin.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created on 2025/5/14.
 * @author Zhongyuan Jin
 */
@RestController
@RequestMapping("/unit")
public class UnitController {
    @Autowired
    private UnitService unitService;

    //获取全部施工单位
    @GetMapping("/all")
    public ResponseResult<List<Unit>> getAllUnitsInfo() {
        List<Unit> units = unitService.list();
        return ResponseResult.success(200, "操作成功", units);
    }

    //查询所有施工单位（支持分页）
    @GetMapping("/list")
    public ResponseResult<List<Unit>> list(@RequestParam(defaultValue = "1") int pageNum,
                                           @RequestParam(defaultValue = "10") int pageSize) {
        Page<Unit> page = new Page<>(pageNum, pageSize);
        List<Unit> records = unitService.page(page).getRecords();
        long total = unitService.page(page).getTotal();
        return ResponseResult.success("获取所有施工单位（支持分页）", records, total);

    }

    //根据 ID 查询施工单位
    @GetMapping("/{id}")
    public ResponseResult<Unit> getById(@PathVariable Integer id) {
        Unit unit = unitService.getById(id);
        if (unit == null) {
            return ResponseResult.fail("未找到该施工单位");

        }
        return ResponseResult.success("根据 ID 获取施工单位", unit);
    }

    //根据  userId 查询施工单位
    @GetMapping("/user/{userId}")
    public ResponseResult<Unit> getUnitByUserId(@PathVariable Long userId) {
        Unit unit = unitService.getUnitByUserId(userId);
        if (unit == null) {
            return ResponseResult.fail("未找到该施工单位");
        }
        return ResponseResult.success("根据 userId 获取施工单位", unit);

    }

    //新增施工单位
    @PostMapping
    public ResponseResult<Boolean> add(@RequestBody Unit unit) {
        boolean result = unitService.save(unit);
        return new ResponseResult<>(result ? 0 : 1, result ? "添加成功" : "添加失败", result,null);
    }

    //更新施工单位信息
    @PutMapping
    public ResponseResult<Boolean> update(@RequestBody Unit unit) {
        boolean result = unitService.updateById(unit);
        return new ResponseResult<>(result ? 0 : 1, result ? "更新成功" : "更新失败", result,null);
    }

    //删除材料信息
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> delete(@PathVariable Integer id) {
        boolean result = unitService.removeById(id);
        return new ResponseResult<>(result ? 0 : 1, result ? "删除成功" : "删除失败", result, null);
    }

    //启用施工单位
    @PutMapping("/enable/{id}")
    public ResponseResult<Boolean> enable(@PathVariable Integer id) {
        Unit unit = unitService.getById(id);
        if (unit == null) {
            return ResponseResult.fail("未找到该施工单位");
        }
        unit.setIsEnable(1); // 启用
        boolean result = unitService.updateById(unit);
        return new ResponseResult<>(result ? 0 : 1, result ? "启用成功" : "启用失败", result,  null);
    }

    //禁用材料
    @PutMapping("/disable/{id}")
    public ResponseResult<Boolean> disable(@PathVariable Integer id) {
        Unit unit = unitService.getById(id);
        if (unit == null) {
            return ResponseResult.fail("未找到该施工单位");

        }
        unit.setIsEnable(0); // 禁用
        boolean result = unitService.updateById(unit);
        return new ResponseResult<>(result ? 0 : 1, result ? "禁用成功" : "禁用失败", result,  null);
    }

    //按名称模糊查询
    @GetMapping("/search")
    public ResponseResult<List<Unit>> searchByName(@RequestParam String unitName) {
        List<Unit> units = unitService.list(new QueryWrapper<Unit>().like("unit_name", unitName));
        return ResponseResult.success( "按名称模糊查询成功", units);

    }


}